<template>
  <div class="skill-list-container">
    <div class="content">
      <!-- 搜索和筛选 -->
      <div class="search-bar">
        <el-input
          v-model="searchKeyword"
          placeholder="搜索技能..."
          prefix-icon="Search"
          size="large"
          @keyup.enter="handleSearch"
          style="max-width: 500px"
        />
        <el-select v-model="selectedCategory" placeholder="选择分类" size="large" @change="handleSearch">
          <el-option label="全部分类" :value="0" />
          <el-option label="学习类" :value="1" />
          <el-option label="生活类" :value="2" />
          <el-option label="艺术类" :value="3" />
          <el-option label="编程类" :value="4" />
          <el-option label="其他" :value="5" />
        </el-select>
        <el-button type="primary" size="large" @click="handleSearch">
          <el-icon><Search /></el-icon>
          搜索
        </el-button>
      </div>

      <!-- 技能列表 -->
      <div class="skill-list" v-loading="loading">
        <div v-for="skill in skillList" :key="skill.id" class="skill-card" @click="viewSkill(skill.id)">
          <div class="skill-cover">
            <img :src="skill.coverImage || '/default-skill.jpg'" alt="">
          </div>
          <div class="skill-info">
            <h3>{{ skill.title }}</h3>
            <p class="description">{{ skill.description }}</p>
            <div class="skill-meta">
              <el-tag size="small">{{ skill.categoryName }}</el-tag>
              <div class="stats">
                <span><el-icon><View /></el-icon> {{ skill.viewCount }}</span>
                <span><el-icon><Star /></el-icon> {{ skill.favoriteCount }}</span>
              </div>
            </div>
            <div class="user-info-small">
              <el-avatar :size="24" :src="skill.userAvatar">{{ skill.userNickname?.[0] }}</el-avatar>
              <span>{{ skill.userNickname }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="fetchSkills"
          @size-change="fetchSkills"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { getSkillList } from '@/api/skill'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const searchKeyword = ref('')
const selectedCategory = ref(0)
const skillList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const fetchSkills = async () => {
  loading.value = true
  try {
    const res = await getSkillList({
      category: selectedCategory.value || undefined,
      keyword: searchKeyword.value || undefined,
      page: currentPage.value,
      size: pageSize.value
    })
    skillList.value = res.data.records || []
    total.value = res.data.total || 0
  } catch (error) {
    console.error('获取技能列表失败：', error)
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  currentPage.value = 1
  fetchSkills()
}

const viewSkill = (id) => {
  router.push(`/skill/detail/${id}`)
}

onMounted(() => {
  fetchSkills()
})
</script>

<style scoped>
.skill-list-container {
  min-height: 100%;
}

.content {
  padding: 30px 20px;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
  animation: fadeIn 0.6s ease-out;
}

.search-bar {
  background: white;
  padding: 28px;
  border-radius: 16px;
  margin-bottom: 28px;
  display: flex;
  gap: 16px;
  align-items: center;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.08);
  border: 1px solid rgba(102, 126, 234, 0.1);
}

.search-bar .el-select {
  min-width: 180px;
}

.search-bar .el-input {
  flex: 1;
}

.skill-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 28px;
}

.skill-card {
  background: white;
  border: 1px solid #f0f2f5;
  border-radius: 16px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
}

.skill-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
  opacity: 0;
  transition: opacity 0.3s;
}

.skill-card:hover::before {
  opacity: 1;
}

.skill-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 16px 40px rgba(102, 126, 234, 0.2);
  border-color: #667eea;
}

.skill-cover {
  width: 100%;
  height: 180px;
  background: linear-gradient(135deg, #f5f7fa 0%, #e8ebf0 100%);
  overflow: hidden;
  position: relative;
}

.skill-cover::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(180deg, transparent 0%, rgba(0, 0, 0, 0.1) 100%);
}

.skill-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

.skill-card:hover .skill-cover img {
  transform: scale(1.1);
}

.skill-info {
  padding: 20px;
  position: relative;
  z-index: 1;
}

.skill-info h3 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #333;
  transition: color 0.3s;
}

.skill-card:hover .skill-info h3 {
  color: #667eea;
}

.description {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 16px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: 44px;
}

.skill-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f2f5;
}

.stats {
  display: flex;
  gap: 16px;
  font-size: 13px;
  color: #999;
  font-weight: 500;
}

.stats span {
  display: flex;
  align-items: center;
  gap: 4px;
  transition: color 0.3s;
}

.stats span:hover {
  color: #667eea;
}

.user-info-small {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: #666;
  font-weight: 500;
}

.user-info-small .el-avatar {
  border: 2px solid #f0f2f5;
  transition: all 0.3s;
}

.skill-card:hover .user-info-small .el-avatar {
  border-color: #667eea;
}

.pagination {
  display: flex;
  justify-content: center;
  padding: 28px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .content {
    padding: 20px 12px;
  }

  .search-bar {
    flex-direction: column;
    padding: 20px;
  }

  .search-bar .el-select,
  .search-bar .el-input {
    width: 100%;
  }

  .skill-list {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}
</style>

